﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class ElementPiece : Item
    {
        
        bool _filterChanged=false;
        bool _isThrow = false;

        public ElementPiece(World world, Stage stage)
            : base(world, stage)
        {
            CurrentAnimation = new Animation("images/items/elementum", new Rectangle(0, 0, 252, 370), new Point(42, 37));
            CurrentAnimation.LayerDepth = 0;
            Size = new Vector2(35, 31);
        }

        public ElementPiece(String Id, World world, Stage stage)
            : base(Id, world, stage)
        {

            CurrentAnimation = new Animation("images/items/elementum", new Rectangle(0, 0, 252, 370), new Point(42, 37));
            CurrentAnimation.DeltaTime = 5;
            if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.ContainsKey(ItemID))
            {
                ItemState itemState = new ItemState();
               
                try
                { itemState.StageID = StageManager.CurrentStateID;
                StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.Add(ItemID, itemState);
                }
                catch (ArgumentException)
                {
                    throw new Exception("An element with Key = "+ItemID +" already exists.");
                }
                
            }

            _isUsed = StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._isUsed;
            _IsCarring = StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._IsCarring;

            if (IsCarring || IsUsed)
            {
                _filter.maskBits = 0x00;
                _owner = GameStage.player;
                if (!_isUsed)
                    GameStage.player.elementPieceList.Add(this);
                if (IsUsed)
                    _body.SetType(BodyType.Static);
                //_fixture.SetFilterData(ref _filter);
            }
            Size = new Vector2(35, 31);

        }

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            _body._userData = "ElementPiece";
            _body._type = BodyType.Dynamic;
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            CurrentAnimation.LayerDepth = 0;
            
            if (IsCarring)
            {
                if (_owner != null)
                {
                    if (_owner.GetBody().GetUserData() == "player")
                    {
                        Player player = (Player)_owner;
                        if (player._playerState == PlayerState.Left)
                        {
                            Position = _owner.Position + new Vector2(-10, -4);
                        }
                        else if (player._playerState == PlayerState.Right)
                        {
                            Position = _owner.Position + new Vector2(10, -4);
                        }
                    }
                }
            }


            if (_filterChanged)
            {
                _fixture.SetFilterData(ref _filter);
                _filterChanged = false;
            }
        }

        public override void OnDraw(SpriteBatch spriteBatch, Matrix transform)
        {
            if (!_IsCarring)
            if(!IsUsed)
            base.OnDraw(spriteBatch, transform);
        }


        public override void ThrowOutItem()
        {
            IsCarring = false;
            _isThrow = true;
            //DataNeedSave.ItemStateTable[ItemID]._IsCarring = false;
            Position = _owner.Position + new Vector2(0,-50);

            Player player = (Player)_owner;

            if (player._playerState == PlayerState.Right)
            {
                ConfigFilter();
                //_filter.maskBits = GroundMask | DefaultCategory;
                _fixture.SetFilterData(ref _filter);
                _body.SetLinearVelocity(new Vector2(_speed, 2.5f));
            }
            if (player._playerState == PlayerState.Left)
            {
                ConfigFilter();
                //_filter.maskBits = GroundMask | DefaultCategory;
                _fixture.SetFilterData(ref _filter);
                _body.SetLinearVelocity(new Vector2(-_speed, 2.5f));
            }

        }

        protected override void ConfigFilter()
        {
            base.ConfigFilter();
            _filter.maskBits = ItemMask ^ MonsterCategory;
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.BeginContactAction(contact, contactBody, normal);
            if (IsUsed)
                return;
            
            if (contactBody.GetUserData() == "machine")
            {
                IsUsed = true;
                IsCarring = false;
                _filter.maskBits = 0x00;
                _fixture.SetFilterData(ref _filter);
            }
            else if (contactBody.GetUserData() == "player" && !_isUsed && !_isThrow)
            {
                _filter.maskBits = 0x00;
                _fixture.SetFilterData(ref _filter);
                IsCarring = true;
                _owner = (Player)contactBody.Owner;
                Player player = (Player)_owner;
                player.elementPieceList.Add(this);
            }
            else if (contactBody.GetUserData() == "ground" && !IsCarring)
            {
                ConfigFilter();
                _filterChanged = true;             
                IsCarring = false;
                IsUsed = false;
                _isThrow = false;
            }

        }
    }
}
